package edu.nf.ch02.common;

import io.minio.MinioClient;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author wangl
 * @date 2025/9/4
 * 便携MinioFactoryBean，用于构建MinioClient和初始化桶
 */
public class MinioFactoryBean implements FactoryBean<MinioOptions> {
    /**
     * 文件服务器连接url
     */
    private String host;
    /**
     * 账号
     */
    private String account;
    /**
     * 密码
     */
    private String password;
    /**
     * 桶名称
     */
    private String bucketName;

    public void setHost(String host) {
        this.host = host;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    @Override
    public Class<?> getObjectType() {
        return MinioOptions.class;
    }

    /**
     * 初始化MinioClient并纳入IOC容器中
     * @return
     * @throws Exception
     */
    @Override
    public MinioOptions getObject() throws Exception {
        //创建MinioClient
        MinioClient client = MinioClient.builder()
                .endpoint(host)
                .credentials(account, password)
                .build();
        //将client封装到MinioOptions中
        return new MinioOptions(client, bucketName);
    }

}